package com.qk.exception;


import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler
    public Result exceptionHandler(Exception e) {
      log.error("程序出现异常",e);
      return Result.error("你的网络有问题，请稍后重试");
    }

    @ExceptionHandler
    public Result handleDuplicateException(DuplicateKeyException e) {
        String message = e.getMessage();
        System.out.println(message);
        if (message.contains("Duplicate entry")) {
            String msg = message.substring(message.indexOf("Duplicate entry"));
            String[] split = msg.split(" ");
            return Result.error(split[2]+"已存在");
        }
        return Result.error("你的网络有问题，请稍后重试");
    }

    @ExceptionHandler
    public Result businessExceptionHandler(BusinessException e) {
        log.error("程序出现异常",e);
        return Result.error(e.getMessage());
    }
}
